const { defineConfig } = require('@vue/cli-service')
const Mock = require('mockjs')

const data = Mock.mock({
  'list|50': [
    {
      'id|+1': 1,
      goodsName: '@ctitle',
      img: '@image(100x100, @color)',
      count: '@integer(1, 3)',
      price: '@integer(1, 100)',
      check: false,
    },
  ],
})

// 这个文件。一旦修改，必须重新启动项目
// vue.config.js => webpack.config.js
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    // 拦截数据，模拟接口 => express
    setupMiddlewares(middlewares, { app }) {
      app.get('/api/list', (req, res) => {
        const { page, size } = req.query
        res.send({
          code: 0,
          msg: 'success',
          data: {
            total: data.list.length,
            list: data.list.slice((page - 1) * size, page * size),
          },
        })
      })

      return middlewares
    },
  },
})
